package com.hwd.gmall.product.controller;

import com.hwd.gmall.common.aspect.HwdCache;
import com.hwd.gmall.model.product.*;
import com.hwd.gmall.product.service.ItemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 内部调用的接口类的控制层
 *
 * @author 黄伟东/Victor
 * @date 2022/4/22 10:15
 */
@RestController
@RequestMapping(value = "/admin/item")
public class ItemController {

    @Resource
    private ItemService itemService;

    /**
     * 根据skuId获取skuInfo信息
     *
     * @param skuId skuInfo表的id
     * @return com.hwd.gmall.model.product.SkuInfo
     */
    @GetMapping(value = "/getSkuInfo/{skuId}")
    @HwdCache(prefix = "getSkuInfo:")
    public SkuInfo getSkuInfo(@PathVariable(value = "skuId") Long skuId) {
        return itemService.getSkuInfo(skuId);
    }

    /**
     * 根据三级分类id获取分类信息（包含一级、二级信息）
     *
     * @param category3Id 三级分类id
     * @return com.hwd.gmall.model.product.BaseCategoryView
     */
    @GetMapping(value = "/getCategory/{category3Id}")
    @HwdCache(prefix = "getCategory:")
    public BaseCategoryView getCategory(@PathVariable(value = "category3Id") Long category3Id) {
        return itemService.getCategory(category3Id);
    }

    /**
     * 根据skuIf查询sku的图片列表
     *
     * @param skuId skuInfo表的id
     * @return java.util.List<com.hwd.gmall.model.product.SkuImage>
     */
    @GetMapping(value = "/listSkuImages/{skuId}")
    @HwdCache(prefix = "listSkuImages:")
    public List<SkuImage> listSkuImages(@PathVariable(value = "skuId") Long skuId) {
        return itemService.listSkuImages(skuId);
    }


    /**
     * 根据spuId和skuId获取商品的销售属性
     *
     * @param spuId spu的id
     * @param skuId sku的id
     * @return java.util.List<com.hwd.gmall.model.product.SpuSaleAttr>
     */
    @GetMapping(value = "/listSpuSaleAttrBySpuIdAndSkuId/{spuId}/{skuId}")
    @HwdCache(prefix = "listSpuSaleAttrBySpuIdAndSkuId:")
    public List<SpuSaleAttr> listSpuSaleAttrBySpuIdAndSkuId(@PathVariable(value = "spuId") Long spuId,
                                                            @PathVariable(value = "skuId") Long skuId) {
        return itemService.listSpuSaleAttrBySpuIdAndSkuId(spuId, skuId);
    }

    /**
     * 根据skuId查询商品价格
     *
     * @param skuId skuInfo表的id
     * @return java.math.BigDecimal
     */
    @GetMapping(value = "/getSkuPrice/{skuId}")
    @HwdCache(prefix = "getSkuPrice:")
    public BigDecimal getSkuPrice(@PathVariable(value = "skuId") Long skuId) {
        return itemService.getSkuPrice(skuId);
    }

    /**
     * 查询指定spu的所有销售属性值与sku对应关系
     *
     * @param spuId spuId
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    @GetMapping(value = "/getSkuIdsValueIdsMap/{spuId}")
    @HwdCache(prefix = "getSkuIdsValueIdsMap:")
    public Map<String, String> getSkuIdsValueIdsMap(@PathVariable(value = "spuId") Long spuId) {
        return itemService.getSkuIdsValueIdsMap(spuId);
    }

    /**
     * 根据品牌id查询品牌信息
     *
     * @param id 品牌表id
     * @return com.hwd.gmall.model.product.BaseTrademark
     */
    @GetMapping(value = "/getBaseTrademark/{id}")
    public BaseTrademark getBaseTrademark(@PathVariable(value = "id") Long id) {
        return itemService.getBaseTrademark(id);
    }

    /**
     * 根据skuId查询商品的平台属性名称和值信息
     *
     * @param skuId shuInfo表的id
     * @return java.util.List<com.hwd.gmall.model.product.BaseAttrInfo>
     */
    @GetMapping(value = "/getBaseAttrInfo/{skuId}")
    public List<BaseAttrInfo> getBaseAttrInfo(@PathVariable(value = "skuId") Long skuId) {
        return itemService.getBaseAttrInfo(skuId);
    }

    /**
     * 扣减库存
     * @param stockDeductMap 扣减的商品id和数量的map对象
     * @return java.lang.Boolean
     */
    @GetMapping(value = "/deductStock")
    public Boolean deductStock(@RequestParam Map<String, Object> stockDeductMap) {
        return itemService.deductStock(stockDeductMap);
    }


    /**
     * 库存回滚
     *
     * @param stockRollbackMap 回滚的商品id和数量的map对象
     * @return java.lang.Boolean
     */
    @GetMapping(value = "/rollbackStock")
    Boolean rollbackStock(@RequestParam Map<String, Object> stockRollbackMap) {
        return itemService.rollbackStock(stockRollbackMap);
    }
}
